<template>
  <div class="login">
    <!--头部栏-->
    <van-nav-bar title="用户登录" left-arrow @click-left="$router.go(-1)" />

    <div v-if="type == false">
      <div class="container">
        <div class="title">
          <h3>账号密码登录</h3>
          <!-- <p>未注册的手机号登录后将自动注册</p> -->
        </div>

        <div class="form">
          <div class="form-item">
            <input v-model="account" class="inp" maxlength="20" placeholder="请输入个人账号" type="text">
          </div>
          <div class="form-item">
            <input v-model="password" class="inp" maxlength="20" placeholder="请输入密码" type="text">
          </div>
        </div>

        <div @click="login" class="login-btn">登录</div>
        <p @click="changeRegist" class="change-btn">没有账号，前往注册</p>
      </div>
    </div>

    <div v-if="type == true">
      <div class="container">
        <div class="title">
          <h3>账号密码注册</h3>
        </div>

        <div class="form">
          <div class="form-item">
            <input v-model="account" class="inp" maxlength="20" placeholder="请输入您的账号" type="text">
          </div>
          <div class="form-item">
            <input v-model="password" class="inp" maxlength="20" placeholder="请输入您的密码" type="text">
          </div>
        </div>

        <div @click="regist" class="login-btn">注册</div>
        <p @click="changeRegist" class="change-btn">立即登录</p>
      </div>
    </div>
  </div>
</template>

<script>
import { codeLogin, userRegist } from '@/api/login'

export default {
  name: 'LoginPage',
  data () {
    return {
      account: '', // 账号
      password: '', // 密码
      /** 是否显示注册，默认是不显示false */
      type: false
    }
  },
  methods: {
    /**
     * 校验账号和密码是否合法
     * 通过校验，返回true
     * 不通过校验，返回false
     */
    validFn () {
      if (!/^.+$/.test(this.account)) {
        this.$toast('请输入账号')
        return false
      }
      if (!/^.+$/.test(this.password)) {
        this.$toast('请输入密码')
        return false
      }
      return true
    },

    /**
     * 登录
     */
    async login () {
      // 先校验
      if (!this.validFn()) {
        return
      }

      console.log('发送登录请求')

      const res = await codeLogin(this.account, this.password) // 请求后端

      console.log(res)

      if (res.status === 200) {
        this.$toast('登录成功')
      } else {
        // 拦截器中会输出后端返回的错误原因，那个Toast会覆盖下面这个，所以用户看不到下面这个
        this.$toast('账号或密码错误！')
        return
      }

      this.$store.commit('user/setUserInfo', res.data) // 将用户的登录信息存入vuex，参考/store/user

      console.log('存好了')

      // 进行判断，看地址栏有无回跳地址
      // 1. 如果有   => 说明是其他页面，拦截到登录来的，需要回跳
      // 2. 如果没有 => 正常去首页
      const url = this.$route.query.backUrl || '/' // 跳转到其他页面或者首页
      this.$router.replace(url)

      console.log('回调了')
    },
    /**
     * 注册
     */
    async regist () {
      // 先校验
      if (!this.validFn()) {
        return
      }

      console.log('发送注册请求')

      const res = await userRegist(this.account, this.password) // 请求后端

      console.log(res)

      if (res.status === 200) {
        this.$toast('注册成功，请去登录吧')
      } else {
        this.$toast('错误发生')
      }
    },
    /**
     * 注册登录切换
     */
    changeRegist () {
      this.type = !this.type
    }
  }
}
</script>

<style lang="less" scoped>
.container {
  padding: 49px 29px;

  .title {
    margin-bottom: 20px;
    h3 {
      font-size: 26px;
      font-weight: normal;
    }
    p {
      line-height: 40px;
      font-size: 14px;
      color: #b8b8b8;
    }
  }

  .form-item {
    border-bottom: 1px solid #f3f1f2;
    padding: 8px;
    margin-bottom: 14px;
    display: flex;
    align-items: center;
    .inp {
      display: block;
      border: none;
      outline: none;
      height: 32px;
      font-size: 14px;
      flex: 1;
    }
    img {
      width: 94px;
      height: 31px;
    }
    button {
      height: 31px;
      border: none;
      font-size: 13px;
      color: #cea26a;
      background-color: transparent;
      padding-right: 9px;
    }
  }

  .login-btn {
    width: 100%;
    height: 42px;
    margin-top: 39px;
    background: linear-gradient(90deg,#ecb53c,#ff9211);
    color: #fff;
    border-radius: 39px;
    box-shadow: 0 10px 20px 0 rgba(0,0,0,.1);
    letter-spacing: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
.change-btn {
    text-align: center;
    margin: 10px 0;
    color: #0B74E6;
    font-size: 14px;
}
</style>
